﻿using System;
using System.IO;
using System.IO.IsolatedStorage; 

namespace tnet.Controls.Lookless
{
    public class Tools2
    { 
        public static bool IsNaN(string DoubleAmount)
        {
            try
            {
                double Temp = double.Parse(DoubleAmount);
                if (Temp > 0)
                {
                    return false;
                }
            }
            catch (Exception) { }
            return true;
        }

        public static int RandomNumber(int min, int max)
        {
            Random random = new Random();
            return random.Next(min, max);
        }

        public static System.Windows.Media.Color GetColor(string ThisColorCode)
        {
            System.Windows.Media.Color c = new System.Windows.Media.Color();
            switch (ThisColorCode.Length)
            {
                case 6:
                    c.A = Convert.ToByte(255);
                    c.R = Convert.ToByte(ThisColorCode.Substring(0, 2), 16);
                    c.G = Convert.ToByte(ThisColorCode.Substring(2, 2), 16);
                    c.B = Convert.ToByte(ThisColorCode.Substring(4, 2), 16);
                    break;
                case 7:
                    c.A = Convert.ToByte(255);
                    c.R = Convert.ToByte(ThisColorCode.Substring(1, 2), 16);
                    c.G = Convert.ToByte(ThisColorCode.Substring(3, 2), 16);
                    c.B = Convert.ToByte(ThisColorCode.Substring(5, 2), 16);
                    break;
                case 8:
                    c.A = Convert.ToByte(ThisColorCode.Substring(0, 2), 16);
                    c.R = Convert.ToByte(ThisColorCode.Substring(2, 2), 16);
                    c.G = Convert.ToByte(ThisColorCode.Substring(4, 2), 16);
                    c.B = Convert.ToByte(ThisColorCode.Substring(6, 2), 16);
                    break;
                case 9:
                    c.A = Convert.ToByte(ThisColorCode.Substring(1, 2), 16);
                    c.R = Convert.ToByte(ThisColorCode.Substring(3, 2), 16);
                    c.G = Convert.ToByte(ThisColorCode.Substring(5, 2), 16);
                    c.B = Convert.ToByte(ThisColorCode.Substring(7, 2), 16);
                    break;
            }
            return c;
        }

        public static String GetSetting(String SomeValue)
        {
            // get saved setting
            StreamReader sr = new StreamReader(new IsolatedStorageFileStream(SomeValue, FileMode.OpenOrCreate, IsolatedStorageFile.GetUserStoreForApplication()));
            String ALine = sr.ReadToEnd();  
            sr.Close(); 
            return ALine;
        }

        public static void SaveSetting(String KeyValue, String SomeValue)
        {
            StreamWriter MyWriter = new StreamWriter(new IsolatedStorageFileStream(KeyValue, FileMode.Create, IsolatedStorageFile.GetUserStoreForApplication()));
            MyWriter.Write(SomeValue);
            MyWriter.Close();
        }
    }
}
